/**
 * 
 */
package pokeronlab_2014;

import javax.swing.JFrame;
import javax.swing.JTextArea;

import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.text.DefaultCaret;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.prefs.InvalidPreferencesFormatException;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/**
 * Az ágens debugolását segítő ablak
 * <p>
 * Parancsok: ExpertSystem on/off, MCTSDecison on/off, exit
 * 
 * @author András
 * 
 */
public class DebugWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7519209070305479280L;
	JTextArea textArea;
	JTextField textField;

	/**
	 * A debug ablakra ír ki egy sort
	 * 
	 * @param msg
	 *            Az ablakra kiírt üzenet
	 * 
	 */
	public void printMessage(String msg) {
		try {
			if(Boolean.parseBoolean(ConfigManager.getInstance().getValue("comment")))
				textArea.append(msg + "\n");
		} catch (InvalidPreferencesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Az ablak bezárása
	 */
	private void close() {
		this.dispose();
	}

	public DebugWindow() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("OnlabBot Debug Window");
		setSize(new Dimension(640, 480));
		setPreferredSize(new Dimension(640, 480));
		getContentPane().setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setAutoscrolls(true);
		getContentPane().add(scrollPane_1);

		// szövegmező beállítása
		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		textArea.setDragEnabled(true);
		textArea.setLineWrap(true);
		textArea.setBackground(Color.DARK_GRAY);
		textArea.setForeground(Color.WHITE);
		textArea.setFont(new Font("Courier New", Font.PLAIN, 12));
		textArea.setEditable(false);
		DefaultCaret caret = (DefaultCaret) textArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		// szövegdoboz hozzáadása és beállítása
		textField = new JTextField();
		textField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (textField.getText().equals("enter command")) {
					textField.setText("");
				}
			}
		});
		// a szövegdobozhoz eventhandlere a parancsok feldolgozásahoz
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				// ha entert nyomunk, a parancs feldolgozása
				if (arg0.getKeyChar() == '\n') {
					try {
						// szavak kiszedése a beírt szövegből
						String[] words = textField.getText().split(" ");
						// config állítása konzolból
						if (words[0].equalsIgnoreCase("set")) {
							ConfigManager.getInstance().setValue(words[1],
									words[2]);
							printMessage(words[1]
									+ " set to " + words[2]);
							// beállítás lekérése
						} else if (words[0].equalsIgnoreCase("print")) {
							printMessage(words[1]
									+ " is "
									+ ConfigManager.getInstance().getValue(
											words[1]));
							// kilépés
						} else if(words[0].equalsIgnoreCase("load")){
							ConfigManager.getInstance().loadConfig(words[1]);
							printMessage("\"" + words[1] + "\" loaded");
						} else if (words[0].equalsIgnoreCase("exit")) {
							close();
						} else {
							printMessage("Invalid command!");
						}
					} catch (IndexOutOfBoundsException e) {
						printMessage("Not enough arguments!");
					} catch (InvalidPreferencesFormatException e) {
						printMessage("Invalid config attribute!");
					}catch (IOException w) {
						printMessage("Invalid file!");
					}
					textField.setText("");
				}
			}
		});
		textField.setBorder(new LineBorder(new Color(171, 173, 179), 2));
		textField.setFont(new Font("Courier New", Font.PLAIN, 12));
		textField.setText("enter command");
		textField.setBackground(Color.DARK_GRAY);
		textField.setForeground(Color.WHITE);
		getContentPane().add(textField, BorderLayout.SOUTH);
		textField.setColumns(10);
		printMessage("Welcome to ÖnlabBot Debug Window");
	}
}
